package com.woniu.user.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.woniu.annotation.RequireRole;
import com.woniu.dto.UserInfoFeignDto;
import com.woniu.exception.user.UserException;
import com.woniu.form.UpdateUserStuInfoForm;
import com.woniu.param.UpdateUserStuInfoParam;
import com.woniu.user.entity.UserInfo;
import com.woniu.user.service.UserInfoService;
import com.woniu.utils.EnumException;
import com.woniu.utils.Result;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.*;


/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author ly
 * @since 2024年12月05日
 */
@RestController
@RequestMapping("/userInfo")
@Api(tags = "用户信息管理")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;
    @GetMapping("/getUserInfo")
    @ApiOperation(value = "查询用户详细信息")
    public Result<?> getUserInfo(@ApiParam("用户编号") @Min(value = 1, message = "id必须为正整数") @Max(value = 999, message = "id不能超过3位") @RequestParam("userId") String userId) throws Exception {
        UserInfo userInfo = userInfoService.getOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUserId, userId).eq(UserInfo::getUserState, "0"));
        if (null == userInfo) {
            throw new UserException(EnumException.USER_NULL_EXCEPTION);
        }
        System.out.println(userInfo);
        UserInfoFeignDto userInfoFeignDto = new UserInfoFeignDto();
        BeanUtil.copyProperties(userInfo, userInfoFeignDto);
        userInfoFeignDto.setUserId(Long.valueOf(userInfo.getUserId()));
        userInfoFeignDto.setRoleId(String.valueOf(userInfo.getRoleId()));
        return Result.getResult(UserInfoFeignDto.class).setCode(200).setMsg("查询成功").setData(userInfoFeignDto);
    }

    @PostMapping("/updateUserStuInfo")
    @ApiOperation(value = "更新学生个人信息")
    public Result updateUserStuInfo(@Valid @RequestBody UpdateUserStuInfoForm updateUserStuInfoForm) throws Exception {
        System.out.println(updateUserStuInfoForm);
        userInfoService.updateUserStuInfo(BeanUtil.toBean(updateUserStuInfoForm, UpdateUserStuInfoParam.class));
        return Result.getResult().setCode(200).setMsg("更新学生个人信息成功");
    }


    @GetMapping("/updateUserStoreInfo")
    @RequireRole("商家")
    @ApiOperation(value = "更新商家个人信息")
    public Result updateUserStoreInfo(@ApiParam("用户id") @Min(value = 1, message = "id必须为正整数") @Max(value = 999, message = "id不能超过3位") @RequestParam Integer userId, @ApiParam("用户手机号") @NotNull(message = "手机号不能为空") @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确")@RequestParam  Integer userPhone, @ApiParam("用户头像") @NotNull(message = "请上传用户头像") @Size(max = 2 * 1024 * 1024, message = "文件大小不能超过2MB") MultipartFile userImag) throws Exception {
        userInfoService.updateUserStoreInfo(userId, userPhone, userImag);
        return Result.getResult().setCode(200).setMsg("更新商家个人信息成功");
    }

    @GetMapping("/changeDefaultAddress")
    @ApiOperation("修改默认地址")
    @RequireRole("学生")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户Id"), @ApiImplicitParam(name = "addressId", value = "要设置默认地址id"), @ApiImplicitParam(name = "addressName", value = "要设置默认地址")})
    public Result changeDefaultAddress
            (@Min(value = 1, message = "用户id必须为正整数") @Max(value = 999, message = "用户id不能超过3位")@RequestParam  Integer
                     userId, @Min(value = 1, message = "地址id必须为正整数") @Max(value = 999, message = "地址id不能超过3位")@RequestParam  Integer
                     addressId, @NotNull(message = "地址信息不能为空") @RequestParam String addressName) throws Exception {
        userInfoService.changeDefaultAddress(userId, addressId, addressName);
        return Result.getResult().setCode(200).setMsg("修改默认地址成功");
    }

}

